package com.jnu.supermaket;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.widget.TableLayout;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        ViewPager2 viewPagerFragments=findViewById(R.id.viewpager2_content);

        viewPagerFragments.setAdapter(new MyFragmentAdapter(this));

        TabLayout tabLayoutHeader=(TableLayout)findViewById(R.id.tablayout_header);
        TabLayoutMediator tabLayoutMediator=new TabLayoutMediator(tabLayoutHeader,viewPagerFragments,

                public void onConfigureTab(TabLayout.Tab tab,int position){
               tab.setText("biaoyi");
        }

                )

    }

    private class MyFragmentAdapter extends FragmentStateAdapter {
        public MyFragmentAdapter(@NonNull FragmentActivity fragment) {
            super(fragment);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {//
            switch (position) {
                case 0:
                return ShopItemFragment.newInstance("test1", "test2");
                default:
                    return WebViewFragment.newInstance("test1", "test2");
            }
        }

        @Override
        public int getItemCount() {
            return 2;
        }
    }
}